#coding:utf8
#
#设备变更信息查询
#
import cherrypy
import datetime
import os
from ConnectMSSQL import ms

from head import print_head
from end import print_end

from utf8code import *
from xxtj import *

_js = """
        <script type="text/javascript">
            var sbbh;
            $(function()
            {
                //开始隐藏层
                $(".bgsbxx_div").hide();
                
                //高亮显示
                $(".table tr").hover
                (
                    function() { $(this).addClass("highlight"); },
                    function() { $(this).removeClass("highlight"); }
                );
                //单选框默认选中.变色.
                $('.table input[type="radio"]:checked').parents('tr').addClass('selected');
                
                //单击
                $('.table tr').click(function()
                {
                    $(this).siblings().removeClass('selected');
                    $(this).addClass('selected');
                    sbbh=$(this).find('td[id="td_sbbh"]').text();
                    sblx=$(this).find('td[id="td_sblx"]').text();
                    
                    //ajax为div层添加数据
                    var xmlhttp;
                    xmlhttp=new XMLHttpRequest();//仅支持 IE7+, Firefox, Chrome, Opera, Safari
                
                    xmlhttp.onreadystatechange=function()
                    {
                      if (xmlhttp.readyState==4 && xmlhttp.status==200)
                        {
                            if(xmlhttp.responseText)
                            {
                                document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
                                //显示层
                                $(".bgsbxx_div").show();
                            }
                        }
                    }
                    xmlhttp.open("GET","/sbbgjl/showOtherInfo?sbbh="+sbbh+"&sblx="+sblx,true);
                    xmlhttp.send();
                });
                
                //鼠标离开这行数据
                $('.table tr').mouseleave(function()
                {
                    //隐藏层
                    $(".bgsbxx_div").hide();
                });
                
                //双击一列
                $('.table tr').dblclick(function()
                {
                    if(sbbh)
                        window.open("/sbbgxx/serchBG?cxtj=" + sbbh);
                    else
                        alert("请选择一台设备");
                });
            });
        </script>
    """

_top = """
    <form method="get" action="serchSB">
        <div id="div_operate">
                                查询条件：
            <input type="text" id="serchtext" name="cxtj" onfocus="hidevalue()"  placeholder="设备编号、固定资产编号、设备名称、品牌型号、MAC地址、使用人" />
            <input type="submit" id="serchbut "value="查询" />
            <input type="button" id="exitbut" onclick="exit()" value="关闭" />
        </div>
        
        <div class="bgsbxx_div" id="myDiv">
        </div>
        
        <div id="div_data">
            <table width="auto" class="table">
              <tr>
                <th class="class_th" scope="col">序号</th>
                <th class="class_th" scope="col">设备编号↓</th>
                <th class="class_th" scope="col">资产编号</th>
                <th class="class_th" scope="col">设备类型</th>
                <th class="class_th" scope="col">型号及品牌</th>
                <th class="class_th" scope="col">MAC</th>
                <th class="class_th" scope="col">使用变更记录</th>
              </tr>
          """
            
_end = """
            </table>
        </div>
    </form>
"""

class SBBGJL:
    _mid_init = """
        <tr class="class_tr">
            <td>%s</td>
            <td id="td_sbbh">%s</td>
            <td>%s</td>
            <td id="td_sblx">%s</td>
            <td>%s</td>
            <td>%s</td>
            <td>%s</td>
        </tr>
    """
    @cherrypy.expose
    def index(self):
        sql = """
            SELECT A.SBBH,
                A.GDZCBH,
                A.SBLX,
                A.PPXH,
                A.WLPZ,
                B.BGJL
            FROM T_SB A
            LEFT JOIN (SELECT SBBH,BGJL=DBO.TJBGJL(SBBH) FROM V_BGJL GROUP BY SBBH) B ON A.SBBH = B.SBBH
            ORDER BY A.SBBH DESC;
        """
        dataList = ms.ExecQuery(sql)
        _mid = ''
        i = 0
        for bgjl in dataList:
            i = i + 1
            _mid += self._mid_init % (i,bgjl[0],bgjl[1],bgjl[2],bgjl[3],bgjl[4],bgjl[5])
        return utf8code(print_head % ('设备变更记录查询','设备变更记录查询') + _js + _top + _mid + _end + div_xxtj % (cherrypy.session.get('user'),len(dataList)) + print_end)
    
    @cherrypy.expose
    def serchSB(self,cxtj):
        sql = """
            SELECT A.SBBH,
                A.GDZCBH,
                A.SBLX,
                A.PPXH,
                A.WLPZ,
                B.BGJL
            FROM T_SB A
            LEFT JOIN (SELECT SBBH,BGJL=DBO.TJBGJL(SBBH) FROM V_BGJL GROUP BY SBBH) B ON A.SBBH = B.SBBH
            WHERE A.SBBH LIKE '%""" + cxtj + "%' or " \
                + "A.GDZCBH LIKE '%" + cxtj + "%' or " \
                + "A.SBMC LIKE '%" + cxtj + "%' or " \
                + "A.PPXH LIKE '%" + cxtj + "%' or " \
                + "A.WLPZ LIKE '%" + cxtj + "%' or " \
                + "B.BGJL LIKE '%" + cxtj + "%'" \
            + 'ORDER BY A.SBBH DESC;'
        dataList = ms.ExecQuery(sql)
        _mid = ''
        i = 0
        for bgjl in dataList:
            i = i + 1
            _mid += self._mid_init % (i,bgjl[0],bgjl[1],bgjl[2],bgjl[3],bgjl[4],bgjl[5])
        return utf8code(print_head % ('设备变更记录查询','设备变更记录查询') + _js + _top + _mid + _end + div_xxtj % (cherrypy.session.get('user'),len(dataList)) + print_end)
    
    @cherrypy.expose
    def showOtherInfo(self,sbbh,sblx):
        showHtml_tmp = ''#设备的其他信息html串
        showHtml = """
            <table width="auto">
                <tr>
                    <th class="class_th" scope="col">购买日期</th>
                    <td colspan="5">%s</td>
                </tr>
                <tr>
                    <th class="class_th" scope="col">设备名称</th>
                    <td colspan="5">%s</td>
                </tr>
                <tr>
                    <th class="class_th" scope="col">基本配置</th>
                    <td colspan="5">%s</td>
                </tr>
                <tr>
                    <th class="class_th" scope="col">随机附件</th>
                    <td colspan="5">%s</td>
                </tr>
                <tr>
                    <th class="class_th" scope="col">设备备注</th>
                    <td colspan="5">%s</td>
                </tr>
            </table>
        """
        sql_show = """
            SELECT GMRQ,SBMC,JBPZ,SJFJ,BZ
            FROM T_SB
            WHERE SBBH = '%s'
        """
        dataList = ms.ExecQuery(sql_show % sbbh)
        for qyxx in dataList:
            showHtml_tmp += showHtml % (datetime.datetime.strftime(qyxx[0],'%Y-%m-%d'),qyxx[1],qyxx[2],qyxx[3],qyxx[4])

        #=================================设备变更信息的显示=========================================        
        bgjlHtml_s = """      
            <table width="auto">
              <tr>
                <th class="class_th" scope="col">#</th>
                <th class="class_th" scope="col">部门</th>
                <th class="class_th" scope="col">人员</th>
                <th class="class_th" scope="col">变更日期</th>
                <th class="class_th" scope="col">经手人</th>
                <th class="class_th" scope="col">变更备注</th>
              </tr>
        """
        bgjlHtml_e = """
            </table>
        """    
        _mid_tmp = """            
                    <tr>
                        <td>%s</td>
                        <td>%s</td>
                        <td>%s</td>
                        <td>%s</td>
                        <td>%s</td>
                        <td>%s</td>
                    </tr>
                    """
        sql_bgjl = """
            SELECT B.BM,B.RY,A.BGRQ,A.JSR,A.BZ FROM T_SBBG A
            LEFT JOIN T_BMRY B ON A.RYID = B.ID
            WHERE A.SBBH = '%s'
            ORDER BY A.BGRQ
        """        
        dataList_2 = ms.ExecQuery(sql_bgjl % sbbh)
        _mid_2 = ''
        i = 0
        for bgjl in dataList_2:
            i += 1
            _mid_2 += _mid_tmp % (i,bgjl[0],bgjl[1],datetime.datetime.strftime(bgjl[2],'%Y-%m-%d'),bgjl[3],bgjl[4])
        
        showHtml_tmp += bgjlHtml_s + _mid_2 + bgjlHtml_e
         
        return utf8code(showHtml_tmp)